package random.fox.p2p.presenter;

import android.text.TextUtils;
import android.util.Log;
import android.widget.AbsListView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import random.fox.p2p.bean.Dynamic;
import random.fox.p2p.bean.Fans;
import random.fox.p2p.bean.PersonalityLabel;
import random.fox.p2p.bean.UserInfo;
import random.fox.p2p.bean.greendao.User;
import random.fox.p2p.interactors.OnLoadNetDataListener;
import random.fox.p2p.interactors.UserInfoInteractor;
import random.fox.p2p.ui.customview.DynamicTimeLineAdapter;
import random.fox.p2p.ui.customview.FansAdapter;
import random.fox.p2p.ui.customview.PersonalityAdapter;
import random.fox.p2p.ui.customview.UserInfoAdapter;
import random.fox.p2p.util.Constants;
import random.fox.p2p.view.IUserInfoView;

/**
 * Created by acer on 2015/3/22.
 */
public class UserInfoPresenterImpl implements UserInfoPresenter, OnLoadNetDataListener {


    private IUserInfoView iUserInfoView;
    private UserInfoInteractor userInfoInteractor;
    private AbsListView listView;
    private int width, height;
    private Constants.TYPE type;

    public UserInfoPresenterImpl(IUserInfoView iUserInfoView, UserInfoInteractor userInfoInteractor) {
        this.iUserInfoView = iUserInfoView;
        this.userInfoInteractor = userInfoInteractor;
    }

    @Override
    public void loadData(AbsListView listView, int width, int height, Constants.TYPE type) {
        this.listView = listView;
        this.width = width;
        this.height = height;
        this.type = type;

        User user = userInfoInteractor.getUser();
        String birthday = user.getBirthday();
        String city = user.getCity();
        String school = user.getSchool();
        String ageStr = null;
        if(TextUtils.isEmpty(birthday)){
            ageStr = "未知";
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String cYear = sdf.format(new Date()).substring(0, 4);
            String birthYear = birthday.substring(0, 4);
            int age = Integer.parseInt(cYear) - Integer.parseInt(birthYear);
            ageStr = age+"岁";
        }
        if(TextUtils.isEmpty(city)) {
            city = "未知";
        }
        if(TextUtils.isEmpty(school)){
            school = "未知";
        }

        iUserInfoView.fillData(ageStr, city, school);

        //TODO 这个方法也可以认为是从本地数据库获得的缓存数据,主要看怎么实现
        UserInfo userInfo = userInfoInteractor.getCacheUserInfo();
        if (userInfo == null) {
            iUserInfoView.showProgress();
            userInfoInteractor.getData(this);
        } else if (listView.getAdapter() == null) { //有数据，但是界面没有adapter
            onSuccess(userInfo);
        }
    }


    @Override
    public void onSuccess(Object o) {
        UserInfo userInfo = (UserInfo) o;
        switch (type) {
            case MIXED:
                UserInfoAdapter adapter = new UserInfoAdapter(userInfo, width, height);
                listView.setAdapter(adapter);
                break;
            case EXPAND_PERSONALITY:
                List<PersonalityLabel> labels = userInfo.getPersonalityLabels();
                listView.setAdapter(new PersonalityAdapter(labels, labels.size()));
                break;
            case EXPAND_DYNAMIC:
                List<Dynamic> dynamics = userInfo.getDynamics();
                listView.setAdapter(new DynamicTimeLineAdapter(dynamics));
                break;
            case EXPAND_FANS:
                List<Fans> fans = userInfo.getFansList();
                listView.setAdapter(new FansAdapter(fans, width / 4, fans.size(), false));
                break;
        }

        iUserInfoView.hideProgress();
    }

    @Override
    public void onFailed(String msg) {
        Log.d("AAA", "onFailed:" + msg);
    }


    /**
     * 释放对象
     */
    public void releaseData() {
        userInfoInteractor.releaseData();
    }
}
